package com.ruoyi.lxy.controller;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.domain.vo.SysUserImportVo;
import com.ruoyi.system.listener.SysUserImportListener;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lxy.domain.vo.TBulletinVo;
import com.ruoyi.lxy.domain.bo.TBulletinBo;
import com.ruoyi.lxy.service.ITBulletinService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 公告
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lxy/bulletin")
public class TBulletinController extends BaseController {

    private final ITBulletinService iTBulletinService;
    private final ISysOssService iSysOssService;

    /**
     * 查询公告列表
     */
    @SaCheckPermission("lxy:bulletin:list")
    @GetMapping("/list")
    public TableDataInfo<TBulletinVo> list(TBulletinBo bo, PageQuery pageQuery) {
        return iTBulletinService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出公告列表
     */
    @SaCheckPermission("lxy:bulletin:export")
    @Log(title = "公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TBulletinBo bo, HttpServletResponse response) {
        List<TBulletinVo> list = iTBulletinService.queryList(bo);
        ExcelUtil.exportExcel(list, "公告", TBulletinVo.class, response);
    }

    /**
     * 获取公告详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("lxy:bulletin:query")
    @GetMapping("/{id}")
    public R<TBulletinVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTBulletinService.queryById(id));
    }


    /**
     * 新增公告
     */
    @SaCheckPermission("lxy:bulletin:add")
    @Log(title = "公告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TBulletinBo bo) {
        if(bo.getOssIds()!=null)
            bo.setFilename(iSysOssService.getFilename(bo.getOssIds()));
        return toAjax(iTBulletinService.insertByBo(bo));
    }




    /**
     * 修改公告
     */
    @SaCheckPermission("lxy:bulletin:edit")
    @Log(title = "公告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TBulletinBo bo) {
        return toAjax(iTBulletinService.updateByBo(bo));
    }

    /**
     * 删除公告
     *
     * @param ids 主键串
     */
    @SaCheckPermission("lxy:bulletin:remove")
    @Log(title = "公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        //根据id搜索公告
        List<TBulletinVo> vos=iTBulletinService.getOssIds(Arrays.asList(ids), true);

        //查询公告的ossIds并删除文件
        for(int i=0;i<vos.size();i++){
            String oss= vos.get(i).getOssIds();

            //根据逗号分割不同的ossId
            if(oss!=null){
                String[] ossIds=oss.split(",");
                for (String s:ossIds){
                    toAjax(iSysOssService.deleteWithValidByIds(Arrays.asList(Long.valueOf(s)), true));
                }
            }

        }
        return toAjax(iTBulletinService.deleteWithValidByIds(Arrays.asList(ids), true));
    }



    @SaCheckPermission("system:oss:download")
    @GetMapping("/download/{ossId}")
    public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException {
        iSysOssService.download(ossId,response);
    }



}
